<?php

namespace app\admin\model;

use app\admin\common\Common;
use think\captcha\Captcha;
use think\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\exception\DbException;
use think\exception\PDOException;
use think\facade\Session;
use think\Model;
use think\response\Json;

class AdminModel extends Model
{
    /**
     * @param $data
     * 登录请求
     * @return Json
     * @throws Exception
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     * @throws PDOException
     */
    public static function Login($data){
        if (empty($data['username']) || empty($data['password']) || empty($data['captcha']) ){
            return Common::result(400,'参数缺失');
        }
        $captcha = new Captcha();
        if (!$captcha->check($data['captcha'])){
            return Common::result(400,'验证码错误');
        }
        $admin = Db::name('admin')->where('username',$data['username'])->find();
        if ($admin == null){
            return Common::result(400,'不存在此账号');
        }else{
            if ($admin['password'] == md5($data['password'])){
                $salt = Common::getchar(6);
                Session::set('adminid',$admin['id']);
                Session::set('adminusername',$data['username']);
                Session::set('admintoken',md5(md5($data['password']).$salt));
                Session::set('adminqq',$admin['userqq']);
                Session::set('adminname',$admin['nickname']);
                Session::set('adminkey',$admin['admintoken']);
                Db::name('admin')->where('username',$data['username'])->update(['salt' => $salt]);
                return Common::result(200,'登录成功');
            }else{
                return Common::result(400,'密码错误');
            }
        }
    }

    /**
     * 修改个人信息
     */
    public static function changeinfo($data){
        if (empty($data['username']) || empty($data['nickname']) || empty($data['userqq']) ){
            return Common::result(400,'参数缺失');
        }
        $adminid = Session::get('adminid');
        $update = [
            'username' => $data['username'],
            'userqq' => $data['userqq'],
            'nickname' => $data['nickname'],
        ];
        try {
            $admininfo = Db::name('admin')->where('id', $adminid)->find();
            $adminoldtoken = Session::get('admintoken');
            $adminnewtoken = md5($admininfo['password'].$admininfo['salt']);
            if ($adminoldtoken == $adminnewtoken){
                try {
                    $admin = Db::name('admin')->where('id', $adminid)->update($update);
                } catch (PDOException $e) {
                     return Common::result(400,$e->getMessage());
                } catch (Exception $e) {
                     return Common::result(400,$e->getMessage());
                }
                if ($admin>0){
                    Session::set('adminusername',$data['username']);
                    Session::set('adminqq',$data['userqq']);
                    Session::set('adminname',$data['nickname']);
                    return Common::result(200,'修改成功');
                }else{
                    return Common::result(400,'并未修改内容');
                }
            }else{
                return Common::result(400,'参数缺失');
            }
        } catch (DataNotFoundException $e) {
             return Common::result(400,$e->getMessage());
        } catch (ModelNotFoundException $e) {
             return Common::result(400,$e->getMessage());
        } catch (DbException $e) {
             return Common::result(400,$e->getMessage());
        }
    }

    /**
     * @param $data
     * 修改密码
     * @return Json|void
     * @throws DataNotFoundException
     * @throws DbException
     * @throws Exception
     * @throws ModelNotFoundException
     * @throws PDOException
     */
    public static function changepwd($data){
        if (empty($data['oldpwd']) || empty($data['newpwd'])){
            return Common::result(400,'参数缺失');
        }
        $adminid = Session::get('adminid');
        $admininfo = Db::name('admin')->where('id',$adminid)->find();
        $adminoldtoken = Session::get('admintoken');
        $adminnewtoken = md5($admininfo['password'].$admininfo['salt']);
        if ($adminoldtoken == $adminnewtoken){
            if ($admininfo['password'] == md5($data['oldpwd'])){
                $admin = Db::name('admin')->where('id',$adminid)->update(['password' => md5($data['newpwd'])]);
                if ($admin>0){
                    Session::clear();
                    return Common::result(200,'修改成功');
                }
            }else{
                return Common::result(400,'旧密码错误');
            }
        }else{
            return Common::result(400,'参数缺失');
        }
    }
}